package com.lv.suyiyong.hfr.ui

import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lv.common.base.BaseActivity
import com.lv.common.ktx.initSingleClickListener
import com.lv.common.network.model.DataResult
import com.lv.common.utils.ARoutUtils
import com.lv.common.utils.LiveEventUtils
import com.lv.common.utils.StatusBarUtil
import com.lv.service.model.EditorViewModel
import com.lv.service.model.ServiceViewModel
import com.lv.service.net.VerifyPhoneReqBody
import com.lv.service.network.BaseYgxRsp
import com.lv.service.network.onBizError
import com.lv.service.network.onBizOK
import com.lv.service.network.onSuccess
import com.lv.service.repo.UserInfo
import com.lv.suyiyong.hfr.R
import com.lv.suyiyong.hfr.bean.TokenBean
import com.lv.suyiyong.hfr.databinding.ActivityForgetFristBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 * 作者： ygx
 * 创建日期：2023/3/2 7:26 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：忘记密码
 */
@Route(path = ARoutUtils.ForgetPasswordFristActivity)
class ForgetPasswordFristActivity : BaseActivity<ActivityForgetFristBinding>(),
    View.OnClickListener {
    private val serviceViewModel: ServiceViewModel by viewModel()
    private val mEditorViewModel by viewModels<EditorViewModel>()
    private val myScope: CoroutineScope = CoroutineScope(SupervisorJob())
    override fun getLayoutRes() = R.layout.activity_forget_frist
    override fun initView() {
        super.initView()
        initStatusBar(this, true, true)

        mBinding.apply {
            vm = mEditorViewModel
            StatusBarUtil.setHeightAndPadding(this@ForgetPasswordFristActivity, titleBar)
            titleBar.onClickBackIcon { finish() }
            titleBar.setTitle("忘记密码")
            initSingleClickListener(btnLogin)

        }
    }


    override fun initConfig() {
        super.initConfig()


    }


    override fun initData() {
        super.initData()
        LiveEventBus
            .get(LiveEventUtils.FINISH_SETPASSWORDACTIVITY_KEY, String::class.java)
            .observe(this) {
                finish()
            }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_login -> {
                checkRegister(
                    VerifyPhoneReqBody(mEditorViewModel.obMobile.get()!!, "0")
                )
            }
        }
    }

    private fun checkRegister(body: VerifyPhoneReqBody) {
        startLoading()
        lifecycleScope.launchWhenCreated {
            val result: DataResult<BaseYgxRsp> =
                myScope.async { serviceViewModel.verifyPhone(body) }
                    .await()
            parseVerifyPhoneResult(result)
        }
    }

    private fun parseVerifyPhoneResult(data: DataResult<BaseYgxRsp>) {
        loadFinished()
        data.onSuccess {
            onBizOK<UserInfo> { code, data, message, result ->
                ARouter.getInstance().build(ARoutUtils.ForgetPasswordSecoundActivity)
                    .withParcelable("userInfo", data)
                    .navigation()

            }
            onBizError<TokenBean> { code, message, data ->

            }
        }
    }

}